package com.feib.soeasy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.soeasy.dao.PaymentDao;
import com.feib.soeasy.dao.UploadFileDao;
import com.feib.soeasy.model.ColumnDefinition;
import com.feib.soeasy.model.FormatDefinition;
import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.UploadFile;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.pagination.QryDownloadFileList;
import com.feib.soeasy.service.UploadFileService;
import com.feib.soeasy.util.DateUtil;
import com.feib.soeasy.util.UserUtil;


/**
 * @title (#)UploadFileServiceImpl.java<br>
 * @description 使用者上傳紀錄邏輯物件<br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/14 create by Jimmy Liu<br>
 */
@Service("uploadFileService")
public class UploadFileServiceImpl extends BaseServiceImpl implements  UploadFileService {

    @Resource(name="uploadFileDao")
    private UploadFileDao uploadFileDao;

    @Resource(name="paymentDao")
    private PaymentDao paymentDao;

    /**
     * 依據查詢物件與上傳日期起迄查詢上傳紀錄
     * @param uploadFile 查詢條件
     * @param uploadFrom 上傳日期起
     * @param uploadTo 上傳日期迄
     * @param paginationConfig 換頁條件
     * @param fileStatus	
     * @return 查詢結果物件
     */
    @Override
    public PaginatedList<UploadFile> queryByUploadFile(
                    UploadFile uploadFile, Date uploadFrom, 
                    Date uploadTo, PaginatedList<UploadFile> paginationConfig) {

        // 上傳時間查詢條件設定
        if (null != uploadFrom)
        {
            // 將時分秒設定在00:00:00
            uploadFrom = DateUtil.getInstance().getBeginOfDateTime(uploadFrom);
        }

        if (null != uploadTo)
        {
            // 將時分秒設定在23:59:59
            uploadTo = DateUtil.getInstance().getEndOfDateTime(uploadTo);
        }

        // 設定 Default 排序  uploadDate desc
        //if (StringUtils.isEmpty(paginationConfig.getSortCriterion()))
        {
            paginationConfig.setSortCriterion("uploadDate");
            paginationConfig.setSortDirection(PaginatedList.SORT_DIRECTION_DESC);
        }

        return uploadFileDao.queryPaginationByUploadFile(uploadFile, uploadFrom, uploadTo, paginationConfig);
    }

    /**
     * 取得單筆上傳檔案資料
     * @param uploadFilePk 上傳檔pk
     * @return 上傳檔案 UploadFile
     * @author Leo Lee
     * @since 2010/11/25
     */
    public UploadFile getUploadFileByPk(Long uploadFilePk)
    {
        return uploadFileDao.getUploadFileByPk(uploadFilePk);
    }

    /**
     * 更新放行結果
     * @param uploadFilePk 上傳檔案pk key
     * @param fileStatus 放行結果 3: 確認放行, 4: 不放行(表上傳資料有誤)
     */
    public void updateUploadFileStatus(Long uploadFilePk, Integer fileStatus)
    {
        UploadFile uploadFile = getUploadFileByPk(uploadFilePk);
        if(fileStatus.intValue() == 3)
        {
            uploadFile.setFileStatus(fileStatus);
            uploadFile.setConfirmStatus(Boolean.TRUE);
            uploadFile.setConfirmUser(UserUtil.getUser());
            uploadFile.setConfirmDate(new Date());
            uploadFile.setUpdatedBy(UserUtil.getUserId());
            uploadFile.setUpdatedDate(new Date());

        }
        else if(fileStatus.intValue() == 4)
        {
            uploadFile.setFileStatus(fileStatus);
            uploadFile.setConfirmStatus(Boolean.FALSE);
            uploadFile.setUpdatedBy(UserUtil.getUserId());
            uploadFile.setUpdatedDate(new Date());    		
        }
        uploadFileDao.save(uploadFile);
    }

    public UploadFile add(UploadFile uploadFile)
    {
        uploadFile.setUploadFilePk(null);
        uploadFile.setUploadDate(new Date()); // 上傳日期
        uploadFile.setUploadUser(UserUtil.getUser()); // 上傳人員
        uploadFile.setFileStatus(UploadFile.FILE_STATUS_UN_CHECK); //待檢核
        this.uploadFileDao.save(uploadFile);
        return uploadFile;
    }

    @Override
    public List<UploadFile> queryUnConfirmByUploadFile(UploadFile uploadFile) {
        return uploadFileDao.queryUnConfirmByUploadFile(uploadFile);
    }
    
    @Override
    public List<QryDownloadFileList> queryDownloadDetailFile(UploadFile uploadFile,
			Date uploadFrom, Date uploadTo, Integer paymentStatus) {
        

        // 上傳時間查詢條件設定
        if (null != uploadFrom)
        {
            // 將時分秒設定在00:00:00
            uploadFrom = DateUtil.getInstance().getBeginOfDateTime(uploadFrom);
        }

        if (null != uploadTo)
        {
            // 將時分秒設定在23:59:59
            uploadTo = DateUtil.getInstance().getEndOfDateTime(uploadTo);
        }
    	
    	PaginatedList<UploadFile> paginationConfig = new PaginatedList<UploadFile>();  	
    	
    	List<QryDownloadFileList> results = new ArrayList<QryDownloadFileList>();
    	//20110825  帳單明細資料下載查詢修改換頁條件
    	paginationConfig.setPageSize(-1);
    	uploadFile.setFileStatus(4);
    	
    	paginationConfig = uploadFileDao.queryPaginationByUploadFile(
    				uploadFile, uploadFrom, uploadTo, paginationConfig);
   	
    	
    	if(paginationConfig != null && 
    			paginationConfig.getPaginatedList() != null)
    	{
	    	for(Iterator<UploadFile> i=paginationConfig.getPaginatedList().iterator();
	    				i.hasNext();)
	    	{
	    		QryDownloadFileList fileList = new QryDownloadFileList();
	    		UploadFile file = (UploadFile)i.next();
	    		fileList.setUploadFile(file);
	    		fileList.setTotalCount(
	    				paymentDao.getCountItemByUploadFilePk(file.getUploadFilePk(), paymentStatus));
	    		fileList.setSumAmount(
	    				paymentDao.getTotalAmountByUploadFilePk(file.getUploadFilePk(), paymentStatus));
	    		
	    		if(fileList.getTotalCount() != null && fileList.getTotalCount() != 0)
	    			results.add(fileList);
	    	}
    	}
    	
        
    	return results;
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.UploadFileService#countUploadFileByFormatDefinition(com.feib.soeasy.model.FormatDefinition)
     */
    @Override
    public int countUploadFileByFormatDefinition(FormatDefinition formatDefinition) {
        return this.uploadFileDao.countUploadFileByFormatDefinition(formatDefinition);
    }
    
    public int countUploadFileByGroup(Group group) {
        return this.uploadFileDao.countUploadFileByGroup(group);
    }
    
    
}
